@extends('base')

@section('content')
<form class="layui-form" lay-filter="add_form">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">登录账号：</label>
                <div class="layui-input-block">
                    <input type="text" name="account" lay-verify="required|account" placeholder="请输入账号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">账户名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="account_name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属角色：</label>
                <div class="layui-input-block">
                    <select name="role_id" lay-verify="required|number">
                        <option value="0">--请选择角色--</option>@foreach($role as $key => $vo)
                        <option value="{{ $vo['id'] }}">{{ $vo['role_name'] }}</option>@endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">登录密码：</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="required|password" autocomplete="off" placeholder="请输入登录密码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码：</label>
                <div class="layui-input-block">
                    <input type="password" name="password_confirm" lay-verify="required|password|password_confirm" autocomplete="off" placeholder="请再次输入登录密码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">电子邮箱：</label>
                <div class="layui-input-block">
                    <input type="text" name="account_email" lay-verify="email" placeholder="请输入电子邮箱" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号码：</label>
                <div class="layui-input-block">
                    <input type="text" name="account_phone" lay-verify="phone" placeholder="请输入手机号码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否启用：</label>
                <div class="layui-input-block">
                    <input type="radio" name="account_status" value="1" title="启用" checked>
                    <input type="radio" name="account_status" value="2" title="禁用">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit lay-filter="account-save"><i class="layui-icon layui-icon-ok"></i>提交</button>
            <button type="reset" class="pear-btn pear-btn-sm"><i class="layui-icon layui-icon-refresh"></i>重置</button>
        </div>
    </div>
</form>
@endsection

@section('js')
<script>
    layui.use(['form', 'jquery'], function() {
        let form = layui.form;
        let $ = layui.jquery;

        /// 验证
        form.verify({
            account: function(value, item){
                if(!new RegExp("^[a-zA-Z0-9_]{4,16}$").test(value)){
                    return '账号由4到16位字母，数字，下划线组成！';
                }
                return false
            },
            password: function(value, item){
                if(!new RegExp("^.*(?=.{6,})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$").test(value)){
                    return '密码最少6位，至少包括1个大写字母，1个小写字母，1个数字，1个特殊字符！';
                }
                return false
            },
            password_confirm: function (value, item) {
                if (value !== form.val("add_form").password){
                    return '两次输入的密码不一致！'
                }
                return false
            }
        })

        form.on('submit(account-save)', function(data) {
            $.ajax({
                url: '/account/add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(result) {
                    if (result.status) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.layui.table.reload("account-table");
                        });
                    } else {layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
@endsection
